package com.itheima.dbsharding.month.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期Util
 *
 * @author jiangta
 * @version 1.0
 * @date 2020/2/24 下午7:18
 */
public class DateUtils {
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private static final String STANDARD_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static final String YYYYMMDD_PATTERN = "yyyyMMdd";

//    /**
//     * 将时间戳转成时间
//     * @param scanTm
//     * @return
//     */
//    public static String getTime(long scanTm) {
//
//        return DateFormatUtils.format(scanTm,STANDARD_DATETIME_FORMAT);
//    }
//
//    /**
//     * 将日期字符串转换成日期
//     *
//     * @param dateStr
//     * @return
//     */
//    public static Date parseStandardDate(String dateStr) {
//        Date date = null;
//        try {
//            date = org.apache.commons.lang3.time.DateUtils.parseDate(dateStr,STANDARD_DATETIME_FORMAT);
//        } catch (ParseException e) {
//            logger.error("datestr parse to date error", e);
//        }
//        return date;
//    }


    //这两个方法使我们可以方便的实现将旧的日期类转换为新的日期类，具体思路都是通过Instant当中介，然后通过Instant来创建LocalDateTime（这个类可以很容易获取LocalDate和LocalTime），新的日期类转旧的也是如此，将新的先转成LocalDateTime，然后获取Instant，接着转成Date，具体实现细节如下：

    // 01. java.util.Date --> java.time.LocalDateTime
    public static LocalDateTime UDateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        return localDateTime;
    }

    // 02. java.util.Date --> java.time.LocalDate
    public static LocalDate UDateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

    // 03. java.util.Date --> java.time.LocalTime
    public static LocalTime UDateToLocalTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalTime localTime = localDateTime.toLocalTime();
        return localTime;
    }


    // 04. java.time.LocalDateTime --> java.util.Date
    public static Date LocalDateTimeToUdate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }


    // 05. java.time.LocalDate --> java.util.Date
    public static Date LocalDateToUdate(LocalDate localDate) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    // 06. java.time.LocalTime --> java.util.Date
    public static Date LocalTimeToUdate(LocalTime localTime) {
        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * yyyy-MM-dd HH:mm:ss ==> yyyyMMdd
     *
     * @param dateTime
     * @return
     */
    public static String convertToyyyyMMdd(String dateTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(STANDARD_DATETIME_FORMAT);
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, df);
        String yyyyMMdd = localDateTime.format(DateTimeFormatter.ofPattern(YYYYMMDD_PATTERN));
        return yyyyMMdd;
    }
}
